#include <algorithm>
#include <cstdio>
#include <cstring>
#define N 1000
using namespace std;

int n, m, cnt;
int fa[N];
int time[N][N];
int find(int x) {
	return x == fa[x] ? x : find(fa[x]);
}
void init() {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			time[i][j] = 0x3fffffff;
		}
	}
}
void add(int u, int v) {
	fa[u] = find(v);
	time[u][fa[u]] = min(min(time[v][u], time[u][v]), ++cnt);
	time[v][u] = time[u][v];
}

void query(int u, int v) {
	if (time[u][v] == 0x3fffffff)
		printf("0\n");
	else
		printf("%d\n", time[u][v]);
}

int main() {
	freopen("coldwar.in", "r", stdin);
	freopen("coldwar.out", "w", stdout);
	scanf("%d %d", &n, &m);
	init();
	for (int i = 0; i < m; ++i) {
		int tmp, tmp2, tmp3;
		scanf("%d %d %d", &tmp, &tmp2, &tmp3);
		switch (tmp) {
		case 0:
			add(tmp2, tmp3);
			break;
		case 1:
			query(tmp2, tmp3);
			break;
		}
	}
	return 0;
}